package com.tojabo.swing.six_pane;

import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.table.DefaultTableModel;

public class JScrollBarDemo {

	private JFrame frame;
	private JScrollPane scrollPane;
	private JLabel title_lb;
	private JTable table = null;
	private DefaultTableModel defaultModel = null;
	
	private JButton scroll_but;
	private JButton stop_but;
	private JScrollBar scrollBar;
	//private int my_barValue=10;  //我们设置的barValue
	
	private int last_barValue =  -1; //上一次的barValue
	private int cur_barValue =  0; //当前的barValue
	
	private boolean isScroll=false;
		
	static final int WIDTH=350;
    static final int HEIGHT=450;
    
    private int place_left =10;
    private int place_top =20;
    
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		JScrollBarDemo demo=new JScrollBarDemo();
		
		List<PersonVo> peoples = new ArrayList<PersonVo>();
		
		for(int i=0;i<100;i++){
			PersonVo person = new PersonVo("name"+i,"phone"+i,"address"+i);
			peoples.add(person);
		}
		
		demo.showTableContent(peoples);
	}
	
	public JScrollBarDemo(){
						
		frame = new JFrame("测试程序");		
		frame.setSize(WIDTH, HEIGHT);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		title_lb = new JLabel("JScrollBar");
		scroll_but = new JButton("滚动");
		stop_but = new JButton("停止");
		
		//JTextArea ta = new JTextArea("我们是某某软件公司的骨干开发人员，我们会竭诚为您服务！！！");// 创建一个文本域组件和一个滚动条面板，并且将滚动条面板添加到顶层容器内
		
		
		Object[][] p = {
				{ " ", " ", " " }};
		String[] n = { "姓  名", "手机号码", "地    址" };
		defaultModel = new DefaultTableModel(p, n);// 创建一个默认的表格模型
		table = new JTable(defaultModel);

		//table.setRowHeight(32);
		//table.setFont(new Font("宋体",Font.BOLD,26));
		
		
		scrollPane = new JScrollPane(table);
		scrollBar = scrollPane.getVerticalScrollBar(); //得到了该JScrollBar
		
		frame.add(title_lb);
		frame.add(scroll_but);
		frame.add(stop_but);
		frame.add(scrollPane);
		init();
		frame.setVisible(true);
	}
	
	public void init(){
		frame.setLayout(null);
		
		scroll_but.setBounds(place_left, place_top, 80, 25);
		stop_but.setBounds(place_left+100, place_top, 80, 25);
		title_lb.setBounds(place_left, place_top+30, 250, 25);
				
		scrollPane.setBounds(place_left, place_top+70, 200, 300);
		
		scroll_but.addMouseListener(new MouseAdapter(){
			public void mouseClicked(MouseEvent e) {
				title_lb.setText("12121 isScroll:"+isScroll);
				if(isScroll==false){
					isScroll=true;
					ScrollThread scrollThread = new ScrollThread();
					scrollThread.start();
				}
				
				
			}
		});
		
		stop_but.addMouseListener(new MouseAdapter(){
			public void mouseClicked(MouseEvent e) {
				
				if(isScroll==true){
					isScroll=false;
				}
				//title_lb.setText("stop isScroll:"+isScroll);
			}
		});
		
	}
	
	
	public void showTableContent(List<PersonVo> peoples) {

		
		if (peoples.size() > 0) {
			// 增加 奖项 名称
			Vector<String> blank1 = new Vector<String>();
			blank1.add(" ");
			defaultModel.addRow(blank1);
			

			// 增加 奖项 名称
			Vector<String> vname = new Vector<String>();
			vname.add("er deng j");
			defaultModel.addRow(vname);

			// 增加 表格 头
			Vector<String> v = new Vector<String>();
			v.add("姓  名");
			v.add("手机号码 ");
			v.add("地    址");
			defaultModel.addRow(v);

			PersonVo people=null;
			
			// 增加表格 内容
			for (int i = 0; i < peoples.size(); i++) {
				people = peoples.get(i);
				Vector<String> vpeople = new Vector<String>();
				vpeople.add(people.getName());
				vpeople.add(people.getPhone());
				vpeople.add(people.getAddress());
				defaultModel.addRow(vpeople);
			}

		}

	}
	
	class ScrollThread extends Thread{

		@Override
		public void run() {
																				
			while(isScroll){
				
				cur_barValue=scrollBar.getValue();//得到JScrollBar现在的位置
				System.out.println("last_barValue="+last_barValue+" ,cur_barValue="+cur_barValue);
				
				if(last_barValue  != cur_barValue){
					/*int temp=last_barValue-cur_barValue;
					if(temp>100||temp<-100){
						my_barValue=cur_barValue;
					}*/
					
					last_barValue = cur_barValue;
					cur_barValue = cur_barValue +50;
					
					scrollBar.setValue(cur_barValue); //设置一个具体位置，value为具体的位置
					table.repaint(); 
					//table.updateUI();
					title_lb.setText("value:"+cur_barValue);
					
				}else{
					scrollBar.setValue(0);
					last_barValue=-1;
				}
				
				try {
					Thread.sleep(800);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				
			}
						
		}
						
	}

}
